package com.mj.haier.calc.web.rest;
import com.mj.haier.calc.domain.ItfOutcomeStatmentDetailBean;
import com.mj.haier.calc.repository.ItfOutcomeStatmentDetailBeanRepository;
import com.mj.haier.calc.repository.search.ItfOutcomeStatmentDetailBeanSearchRepository;
import com.mj.haier.calc.web.rest.errors.BadRequestAlertException;
import com.mj.haier.calc.web.rest.util.HeaderUtil;
import com.mj.haier.calc.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;

/**
 * REST controller for managing ItfOutcomeStatmentDetailBean.
 */
@RestController
@RequestMapping("/api")
public class ItfOutcomeStatmentDetailBeanResource {

    private final Logger log = LoggerFactory.getLogger(ItfOutcomeStatmentDetailBeanResource.class);

    private static final String ENTITY_NAME = "microCalcItfOutcomeStatmentDetailBean";

    private final ItfOutcomeStatmentDetailBeanRepository itfOutcomeStatmentDetailBeanRepository;

    private final ItfOutcomeStatmentDetailBeanSearchRepository itfOutcomeStatmentDetailBeanSearchRepository;

    public ItfOutcomeStatmentDetailBeanResource(ItfOutcomeStatmentDetailBeanRepository itfOutcomeStatmentDetailBeanRepository, ItfOutcomeStatmentDetailBeanSearchRepository itfOutcomeStatmentDetailBeanSearchRepository) {
        this.itfOutcomeStatmentDetailBeanRepository = itfOutcomeStatmentDetailBeanRepository;
        this.itfOutcomeStatmentDetailBeanSearchRepository = itfOutcomeStatmentDetailBeanSearchRepository;
    }

    /**
     * POST  /itf-outcome-statment-detail-beans : Create a new itfOutcomeStatmentDetailBean.
     *
     * @param itfOutcomeStatmentDetailBean the itfOutcomeStatmentDetailBean to create
     * @return the ResponseEntity with status 201 (Created) and with body the new itfOutcomeStatmentDetailBean, or with status 400 (Bad Request) if the itfOutcomeStatmentDetailBean has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/itf-outcome-statment-detail-beans")
    public ResponseEntity<ItfOutcomeStatmentDetailBean> createItfOutcomeStatmentDetailBean(@Valid @RequestBody ItfOutcomeStatmentDetailBean itfOutcomeStatmentDetailBean) throws URISyntaxException {
        log.debug("REST request to save ItfOutcomeStatmentDetailBean : {}", itfOutcomeStatmentDetailBean);
        if (itfOutcomeStatmentDetailBean.getId() != null) {
            throw new BadRequestAlertException("A new itfOutcomeStatmentDetailBean cannot already have an ID", ENTITY_NAME, "idexists");
        }
        ItfOutcomeStatmentDetailBean result = itfOutcomeStatmentDetailBeanRepository.save(itfOutcomeStatmentDetailBean);
        List<ItfOutcomeStatmentDetailBean> t = new ArrayList<>();
        t.add(result);
        itfOutcomeStatmentDetailBeanSearchRepository.saveAll(t);
        return ResponseEntity.created(new URI("/api/itf-outcome-statment-detail-beans/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /itf-outcome-statment-detail-beans : Updates an existing itfOutcomeStatmentDetailBean.
     *
     * @param itfOutcomeStatmentDetailBean the itfOutcomeStatmentDetailBean to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated itfOutcomeStatmentDetailBean,
     * or with status 400 (Bad Request) if the itfOutcomeStatmentDetailBean is not valid,
     * or with status 500 (Internal Server Error) if the itfOutcomeStatmentDetailBean couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/itf-outcome-statment-detail-beans")
    public ResponseEntity<ItfOutcomeStatmentDetailBean> updateItfOutcomeStatmentDetailBean(@Valid @RequestBody ItfOutcomeStatmentDetailBean itfOutcomeStatmentDetailBean) throws URISyntaxException {
        log.debug("REST request to update ItfOutcomeStatmentDetailBean : {}", itfOutcomeStatmentDetailBean);
        if (itfOutcomeStatmentDetailBean.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        ItfOutcomeStatmentDetailBean result = itfOutcomeStatmentDetailBeanRepository.save(itfOutcomeStatmentDetailBean);
        itfOutcomeStatmentDetailBeanSearchRepository.save(result);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, itfOutcomeStatmentDetailBean.getId().toString()))
            .body(result);
    }

    /**
     * GET  /itf-outcome-statment-detail-beans : get all the itfOutcomeStatmentDetailBeans.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of itfOutcomeStatmentDetailBeans in body
     */
    @GetMapping("/itf-outcome-statment-detail-beans")
    public ResponseEntity<List<ItfOutcomeStatmentDetailBean>> getAllItfOutcomeStatmentDetailBeans(Pageable pageable) {
        log.debug("REST request to get a page of ItfOutcomeStatmentDetailBeans");
        Page<ItfOutcomeStatmentDetailBean> page = itfOutcomeStatmentDetailBeanRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/itf-outcome-statment-detail-beans");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * GET  /itf-outcome-statment-detail-beans/:id : get the "id" itfOutcomeStatmentDetailBean.
     *
     * @param id the id of the itfOutcomeStatmentDetailBean to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the itfOutcomeStatmentDetailBean, or with status 404 (Not Found)
     */
    @GetMapping("/itf-outcome-statment-detail-beans/{id}")
    public ResponseEntity<ItfOutcomeStatmentDetailBean> getItfOutcomeStatmentDetailBean(@PathVariable Long id) {
        log.debug("REST request to get ItfOutcomeStatmentDetailBean : {}", id);
        Optional<ItfOutcomeStatmentDetailBean> itfOutcomeStatmentDetailBean = itfOutcomeStatmentDetailBeanRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(itfOutcomeStatmentDetailBean);
    }

    /**
     * DELETE  /itf-outcome-statment-detail-beans/:id : delete the "id" itfOutcomeStatmentDetailBean.
     *
     * @param id the id of the itfOutcomeStatmentDetailBean to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/itf-outcome-statment-detail-beans/{id}")
    public ResponseEntity<Void> deleteItfOutcomeStatmentDetailBean(@PathVariable Long id) {
        log.debug("REST request to delete ItfOutcomeStatmentDetailBean : {}", id);
        itfOutcomeStatmentDetailBeanRepository.deleteById(id);
        itfOutcomeStatmentDetailBeanSearchRepository.deleteById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/itf-outcome-statment-detail-beans?query=:query : search for the itfOutcomeStatmentDetailBean corresponding
     * to the query.
     *
     * @param query the query of the itfOutcomeStatmentDetailBean search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/itf-outcome-statment-detail-beans")
    public ResponseEntity<List<ItfOutcomeStatmentDetailBean>> searchItfOutcomeStatmentDetailBeans(@RequestParam String query, Pageable pageable) {
        log.debug("REST request to search for a page of ItfOutcomeStatmentDetailBeans for query {}", query);
        Page<ItfOutcomeStatmentDetailBean> page = itfOutcomeStatmentDetailBeanSearchRepository.search(queryStringQuery(query), pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/itf-outcome-statment-detail-beans");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

}
